package com.dream.dream_ioc.beans.factory;

import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wenpan
 */
public class BeanFactoryUtils {

    private static final Map<String, String> transformedBeanNameCache = new ConcurrentHashMap<>();

    /**
     * 将 bean 名称转换为实际的名称，如果时工厂 bean 需要去掉前缀
     *
     * @param name
     * @return
     */
    public static String transformedBeanName(String name) {
        Assert.notNull(name, "'name' must not be null");
        if (!name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {
            return name;
        }
        return transformedBeanNameCache.computeIfAbsent(name, beanName -> {
            do {
                beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());
            } while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));
            return beanName;
        });
    }
}
